package com.ithei.edu.info.manager.dao.impl;


import com.ithei.edu.info.manager.dao.StudentDao;
import com.ithei.edu.info.manager.domain.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentDaoImpl implements StudentDao {
    @Override
    public void addStudent() throws IOException {
        Student stu = new Student();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号:");
        while (true) {
            String sid = sc.next();
            boolean bl = this.selectId(sid);
            if (bl == false) {
                System.out.println("请重新输入学号:");
                continue;
            }
            stu.setSid(sid);
            System.out.println("请您输入姓名:");
            stu.setName(sc.next());
            System.out.println("请您输入年龄:");
            stu.setAge(sc.next());
            System.out.println("请您输入生日:");
            stu.setBirthday(sc.next());
            System.out.println("请您输入地址:");
            stu.setAddress(sc.next());
            break;
        }
        this.BufferedWriterFile(stu);
    }

    //读取文件中的所有相关的student对象
    @Override
    public ArrayList<Student> BufferedReaderFile() throws IOException {
        ArrayList<Student> list = new ArrayList<Student>();
        File file=new File("D:\\下载\\file\\学生管理系统的信息\\student.txt");
        file.createNewFile();
        BufferedReader br = new BufferedReader(new
                FileReader("D:\\下载\\file\\学生管理系统的信息\\student.txt"));
        String[] split;
        String line = null;
        while ((line = br.readLine()) != null) {
            split = line.split(",");
            Student stu = Student.getStudent(split);
            list.add(stu);
        }
        br.close();
        if (list.size()==0) {
            return null;
        }
        return list;
    }

    //写入student对象
    @Override
    public void BufferedWriterFile(Student stu) throws IOException {
        BufferedWriter bw = new BufferedWriter(new
                FileWriter("D:\\下载\\file\\学生管理系统的信息\\student.txt", true));
        bw.write(stu.getSid() + ",");
        bw.write(stu.getName() + ",");
        bw.write(stu.getAge() + ",");
        bw.write(stu.getBirthday() + ",");
        bw.write(stu.getAddress());
        bw.newLine();
        bw.flush();
        bw.close();
    }

    //读取全部student对象
    @Override
    public void selectAll() throws IOException {
        ArrayList<Student> list = this.BufferedReaderFile();
        System.out.println("姓名\t\t学号\t\t年龄\t\t生日\t\t地址\t\t");
        if (list == null) {
            System.out.println("文件没有内容!");
        } else {
            for (Student stu : list) {
                stu.show();
            }
        }
    }

    //删除指定id的对象
    @Override
    public void deleteStudentById() throws IOException {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = this.BufferedReaderFile();
        System.out.println("请输入要删除的学号:");
        while (true) {
            String sid = sc.next();
            boolean bl = this.selectId(sid);
            if (bl) {
                System.out.println("如果你不想找了,请输入: 0,否则请输入任意其他字符!");
                if (sc.next().equals("0")) {
                    break;
                }
                System.out.println("请重新输入学号:");
                continue;
            }
            //System.out.println(list.size());
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getSid().equals(sid)) {
                    list.remove(i);
                    i--;
                }
            }
            this.deleteFile(list);
            break;
        }
    }

    //设置指定id的对象
    @Override
    public void setStudentById() throws IOException {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = this.BufferedReaderFile();
        System.out.println("请输入要修改的学号:");
        while (true) {
            String sid = sc.next();
            boolean bl = this.selectId(sid);
            if (bl) {
                System.out.println("你输入的学号不正确,请重新输入要修改的学号:");
                continue;
            }
            Student stu = new Student();
            stu.setSid(sid);
            System.out.println("请输入要修改的姓名:");
            stu.setName(sc.next());
            System.out.println("请输入要修改的年龄:");
            stu.setAge(sc.next());
            System.out.println("请输入要修改的生日:");
            stu.setBirthday(sc.next());
            System.out.println("请输入要修改的地址:");
            stu.setAddress(sc.next());
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i).getSid().equals(sid)){
                    list.set(i,stu);
                }
            }
            deleteFile(list);
            break;
        }
    }

    //查找学号是否在文件里面
    public boolean selectId(String sid) throws IOException {
        ArrayList<Student> list = this.BufferedReaderFile();
        if(list==null){
            return true;
        }
        for (Student stu : list) {
            if (stu.getSid().equals(sid)) {
                return false;
            }
        }
        return true;
    }

    public void deleteFile(ArrayList<Student> list) throws IOException {
        File file = new File("D:\\\\下载\\\\file\\\\学生管理系统的信息\\\\student.txt");
        //ArrayList<Student> list2 = this.BufferedReaderFile();
        while (true) {
            if (file.exists() && file.isFile()) {
                file.delete();
            } else {
                break;
            }
        }
        for (Student stu : list) {
            this.BufferedWriterFile(stu);
        }
    }

}
